# -*- coding: utf-8 -*-
"""
按钮组组件
"""
from PySide6.QtWidgets import QWidget
from PySide6.QtCore import Signal

from ..base import TgComponentMixin
from ..buttons import TgButton
from ..layouts import TgHBoxLayout


class TgButtonGroup(QWidget, TgComponentMixin):
    """TG按钮组组件 - 水平排列的按钮组"""
    
    button_clicked = Signal(str)  # 发射被点击按钮的文本
    
    def __init__(self, buttons=None, parent=None):
        super().__init__(parent)
        # 初始化TG组件属性
        self._component_name = self.__class__.__name__
        self._is_initialized = False
        self._style_applied = False
        self._theme = 'default'
        self._size_hint = 'medium'
        self._component_parts = {}
        
        # 设置组件特定属性
        self.button_configs = buttons or []
        self.buttons = {}
        self.button_callbacks = {}  # 存储每个按钮的回调函数
        
        # 初始化组件
        self.initialize()
    
    def setup_ui(self):
        """初始化UI"""
        layout = TgHBoxLayout(margins='small', spacing='small')
        
        for config in self.button_configs:
            if isinstance(config, str):
                # 简单字符串配置
                button = TgButton(config)
                callback = None
            elif isinstance(config, dict):
                # 详细配置
                button = TgButton(
                    config.get('text', ''),
                    config.get('theme', 'primary'),
                    config.get('size', 'small')
                )
                callback = config.get('callback', None)
            else:
                continue
            
            # 保存回调函数
            if callback:
                self.button_callbacks[button.text()] = callback
                button.clicked.connect(callback)
            
            # 同时保持原有的信号发射机制
            button.clicked.connect(lambda checked, text=button.text(): self.button_clicked.emit(text))
            
            self.buttons[button.text()] = button
            self.add_component_part(f'button_{button.text()}', button)
            layout.addWidget(button)
        
        self.setLayout(layout)
    
    def apply_style(self):
        """应用样式"""
        # 按钮组的样式通过各个按钮自身的样式来实现
        self._style_applied = True
    
    def add_component_part(self, name, component):
        """添加组件部分"""
        self._component_parts[name] = component
    
    def get_component_part(self, name):
        """获取组件部分"""
        return self._component_parts.get(name)
    
    def get_component_parts(self):
        """获取所有组件部分"""
        return self._component_parts.copy()
    
    def add_button(self, text, theme='primary', size='medium', callback=None):
        """添加按钮"""
        button = TgButton(text, theme, size)
        
        # 如果提供了回调函数，连接到按钮
        if callback:
            self.button_callbacks[text] = callback
            button.clicked.connect(callback)
        
        # 同时保持原有的信号发射机制
        button.clicked.connect(lambda checked, t=text: self.button_clicked.emit(t))
        
        self.buttons[text] = button
        self.add_component_part(f'button_{text}', button)
        layout = self.layout()
        if layout:
            layout.addWidget(button)
    
    def get_button(self, text):
        """获取指定按钮"""
        return self.buttons.get(text)
    
    def set_button_callback(self, text, callback):
        """为指定按钮设置回调函数"""
        if text in self.buttons:
            # 如果之前有回调函数，先断开连接
            if text in self.button_callbacks:
                try:
                    self.buttons[text].clicked.disconnect(self.button_callbacks[text])
                except:
                    pass
            
            # 设置新的回调函数
            self.button_callbacks[text] = callback
            self.buttons[text].clicked.connect(callback)
    
    def remove_button_callback(self, text):
        """移除指定按钮的回调函数"""
        if text in self.button_callbacks:
            try:
                self.buttons[text].clicked.disconnect(self.button_callbacks[text])
                del self.button_callbacks[text]
            except:
                pass 